package offer.day3;

import java.util.Scanner;

public class No4find {
    public static void main(String[] args) {
        No4find f = new No4find();
        Scanner in = new Scanner(System.in);
//        System.out.println("输入数组的行数和列数：");
//        int n = in.nextInt();    //n行
//        int m = in.nextInt();	 //m列
//        int[][] array = new int[n][m];  //输入二维数组
//        //{{1,2,8,9},{2,4,9,12},{4,7,10,13},{6,8,11,15}};
//        System.out.println("输入数组的各个元素");
//        for(int i=0;i<n;i++) {
//            for(int j=0;j<m;j++) {
//                array[i][j]=in.nextInt();
//            }
//        }
        int[][] array = new int[][]{{1, 2, 8, 9}, {2, 4, 9, 12}, {4, 7, 10, 13}, {6, 8, 11, 15}};
        //输出数组元素
        System.out.println("输出数组的各个元素：");
        for (int i = 0; i < array.length; i++) {
            for (int j = 0; j < array[0].length; j++) {
                System.out.print(array[i][j] + " ");
            }
            System.out.println();
        }
        System.out.println("输入要查询的数字：");
        int target = in.nextInt();   //输入为整数
        boolean b = f.find(array, target);
        if (b == true) {
            System.out.println("在这个二维数组中有" + target + "这个整数");
        } else {
            System.out.println("在这个二维数组中没有" + target + "这个整数");
        }
    }

    public boolean find(int[][] arr, int target) {
        if (arr == null) return false;
        int row = 0;
        int column = arr[0].length - 1;
        while (row < arr.length && column >= 0) {
            if (arr[row][column] == target) {
                return true;
            } else if (arr[row][column] > target) {
                column--;
            } else {
                row++;
            }
        }
        return false;
    }
}
